<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
            show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('coupon.quick Search Fields') })"
            @action="baTable.onTableHeaderAction" />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" @action="baTable.onTableAction" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { coupon } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

const { t } = useI18n()
const tableRef = ref()
const optButtons = defaultOptButtons(['edit', 'delete'])
const baTable = new baTableClass(
    new baTableApi(coupon),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('coupon.id'), prop: 'id', align: 'center', width: 70, sortable: 'custom', operator: 'RANGE' },
            { label: t('coupon.coupon_name'), prop: 'coupon_name', align: 'center', 'show-overflow-tooltip': true, 'min-width': 100 },
            {
                label: '加油',
                prop: 'can_oil',
                align: 'center',
                render: 'tag',
                replaceValue: { 0: '不允许', 1: '允许' },
                width: 80,
            },
            {
                label: t('coupon.coupon_type'),
                prop: 'coupon_type',
                align: 'center',
                render: 'customTemplate',
                replaceValue: { 0: t('coupon.coupon_type 0'), 1: t('coupon.coupon_type 1') },
                width: 200,
                operator: false,
                customTemplate: (row: TableRow, field: TableColumn, value: any) => {
                    if (value == 0) {
                        return '开始:' + row.start_time + '<br>结束:' + row.end_time
                    } else {
                        return '领取后' + row.days + '天'
                    }
                },
            },
            { label: t('coupon.days'), prop: 'days', align: 'center', operator: 'RANGE' },
            { label: '派发数量', prop: 'num', align: 'center', operator: 'RANGE' },
            { label: t('coupon.price'), prop: 'price', align: 'center', operator: 'RANGE' },
            { label: t('coupon.condition'), prop: 'condition', align: 'center', operator: 'RANGE' },
            { label: '分享赠送', prop: 'share', align: 'center', render: 'switch' },
            { label: '是否启用', prop: 'switch', align: 'center', render: 'switch' },
            {
                label: t('coupon.createtime'),
                prop: 'createtime',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            {
                label: t('coupon.updatetime'),
                prop: 'updatetime',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            {
                label: t('coupon.delete_time'),
                prop: 'delete_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            { label: t('operate'), fixed: 'right', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, 'goods.best_switch'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { coupon_type: '0', start_time: '0', end_time: '0', days: '0', price: '0.00', condition: '0.00' },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
    name: 'coupon',
})
</script>

<style scoped lang="scss">

</style>
